package com.cuitrip.business.bill;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuitrip.apiservice.OrderBusiness;
import com.cuitrip.component.item.ItemLayout;
import com.cuitrip.component.statelayout.StateLayout;
import com.cuitrip.service.R;
import com.cuitrip.util.PlatformUtil;
import com.cuitrip.util.time.InputTime;
import com.cuitrip.util.time.OutputTime;
import com.cuitrip.util.time.TimeUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;
import com.lab.network.config.CtResponseCode;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.MessageUtils;
import com.lab.utils.NumberUtils;
import com.lab.utils.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by MiJack on 2015/11/6.
 */
public class CashDetailFragment extends CustomUiFragment implements IProxyCallback{
//    "": "1446730584000",
//            "": "个人提现",
//            "": "3",
//            "money": "100.0",
//            "": 2,
//            "paypal": "hhfghhh@ggg.com",
//            "moneyType": "USD"

    public static class  CashDetail{
        public String errorReason;
        public String title;
        public String gmtModified;//1446780500000
        public int status;// "3",
        public String gmtCreated;// "1446730584000",
        public String money;// "100.0",
        public String accountType;// 2,
        public String moneyType;// "USD",
        public String paypal;// public String hhfghhh@ggg.com"
    }
    @Bind(R.id.billState)
    TextView billState;
    @Bind(R.id.moneyType)
    TextView moneyType;
    @Bind(R.id.moneyCount)
    TextView money;
    @Bind(R.id.cash_tip)
    TextView tipCash;
    @Bind(R.id.layoutPaypal)
    ItemLayout layoutPalpal;
    @Bind(R.id.layoutGetCashTime)
    ItemLayout layoutGetCashTime;
    @Bind(R.id.layout)
    ItemLayout layout;
    @Bind(R.id.state_layout)
    StateLayout mStateLayout;

    private String billId;

    private ApiProxy mApiProxy=new ApiProxy(this);

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_cash_detail);
        ButterKnife.bind(this, view);
        billId=getArguments().getString("billId",null);
        if(billId==null){
            mStateLayout.changeState(StateLayout.State.Empty);
            return  view;
        }
        showNoCancelDialog();
        OrderBusiness.getCashDetail(mApiProxy, billId);
        mStateLayout.changeState(StateLayout.State.Loading);
        mStateLayout.setOnClickStateLayoutListener(new StateLayout.OnClickStateLayoutListener() {
            @Override
            public void onClick(StateLayout.State state) {
                OrderBusiness.getCashDetail(mApiProxy,billId);
            }
        }); return  view;
    }

    @Override
    public void onTopbarLeftPress() {
        getActivity().finish();
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mTopbarMiddleAreaName = getString(R.string.withdraw_detail);
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        config.mTopbarStyle = CustomUiConfig.BarStyle.WHITE_STYLE;
        return config;
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        CtApiResponse response=(CtApiResponse)result.getData();
        if(response!=null && response.code== CtResponseCode.RESULT_OK){
            hideNoCancelDialog();
            if(response.result instanceof CashDetail){
                mStateLayout.changeState(StateLayout.State.INIT);
                CashDetail detail= (CashDetail) response.result;
                //通用的payPal
                layoutPalpal.setmRightText(Utils.formatPaypal(detail.paypal));
                money.setText("- " + NumberUtils.getMoneyFormat(detail.money));
                moneyType.setText(detail.moneyType);
                layoutGetCashTime.setmRightText(
                        TimeUtil.formatTime(
                                InputTime.build(detail.gmtCreated),
                                OutputTime.getLocalInstance(OutputTime.OutputType.Type_Minute))
                );
                switch (detail.status){
                    case 1:
                        //处理中
                        billState.setText(R.string.withdraw_state_process);
                        tipCash.setVisibility(View.GONE);
                        layout.setVisibility(View.VISIBLE);
                        layout.setleftText(getString(R.string.withdraw_attribute_approximate_time));
                        //layout.setmRightText(getString(R.string.duration_content,"3~4"));
                        layout.setmRightText(getString(R.string.withdraw_approximate_time));
                        break;
                    case 2:
                        //提现完成
                        layout.setleftText(getString(R.string.withdraw_attribute_complete_time));
                        billState.setText(R.string.withdraw_state_complete);
                        tipCash.setVisibility(View.GONE);
                        layout.setmRightText(
                                TimeUtil.formatTime(
                                InputTime.build(detail.gmtModified),
                                OutputTime.getLocalInstance(OutputTime.OutputType.Type_Minute))
                        );
                        break;
                    case 3:
                        //提现失败
                        billState.setText(R.string.withdraw_state_fail);
                        if (TextUtils.isEmpty(detail.errorReason)){
                            tipCash.setVisibility(View.GONE);
                        }else{
                            tipCash.setText(detail.errorReason);
                            tipCash.setVisibility(View.VISIBLE);
                        }
                        layout.setVisibility(View.GONE);
                        break;
                }
            }else{
                mStateLayout.changeState(StateLayout.State.NoNetwork);
                hideNoCancelDialog();
                String msg;
                if (response != null && !TextUtils.isEmpty(response.msg)) {
                    msg = response.msg;
                } else {
                    msg = PlatformUtil.getInstance().getString(R.string.feedback_data_err);
                }
                MessageUtils.showToast(msg);
            }
        }

        return false;
    }
}
